/**
 * 
 */
package com.hanhai.zrb.api.biz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.hanhai.zrb.api.biz.dao.mybatis.UserInfoDao;
import com.hanhai.zrb.api.biz.service.UserInfoService;
import com.hanhai.zrb.model.user.UserInfo;
import com.hanhai.zrb.model.user.enums.UserInfoName;
import com.hanhai.zrb.model.user.vo.UserInfoVO;

/**
 * @author ruiqi
 *
 */
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {

	private final Logger logger = LoggerFactory.getLogger(UserInfoServiceImpl.class);

	@Resource
	UserInfoDao userInfoDao;

	@Override
	public UserInfoVO findByEnumNames(String userId, List<String> queryNameList) {
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		userInfo.setQueryNameList(queryNameList);
		List<UserInfo> list = userInfoDao.findByNames(userInfo);
		Map<String, String> infoMap = new HashMap<String, String>();
		for (UserInfo info : list) {
			infoMap.put(info.getInfoName(), info.getInfoValue());
		}
		UserInfoVO userInfoVO = new UserInfoVO();
		userInfoVO.setUserId(userId);
		userInfoVO.setInfoMap(infoMap);
		return userInfoVO;
	}

	@Override
	public UserInfoVO findByNameEnums(String userId,
			List<UserInfoName> queryEmunList) {
		List<String> queryNameList = new ArrayList<String>();
		for (UserInfoName name : queryEmunList) {
			queryNameList.add(name.name());
		}
		return this.findByEnumNames(userId, queryNameList);
	}

	@Override
	public void batchSave(String userId, List<UserInfo> userInfoList) {
		for (UserInfo userInfo : userInfoList) {
			userInfo.setUserId(userId);
		}
		userInfoDao.batchSave(userInfoList);
	}

	@Override
	public void batchSave(String userId,
			TreeMap<UserInfoName, String> infoValueMap) {
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();

		for (Map.Entry<UserInfoName, String> entry : infoValueMap.entrySet()) {
			UserInfo userInfo = new UserInfo();
			userInfo.setUserId(userId);
			userInfo.setInfoName(entry.getKey().name());
			userInfo.setInfoValue(entry.getValue());
			userInfoList.add(userInfo);
		}
		userInfoDao.batchSave(userInfoList);
	}

	@Override
	public void batchSave(UserInfoVO userInfoVO) {
		String userId = userInfoVO.getUserId();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for (Map.Entry<String, String> entry : userInfoVO.getInfoMap()
				.entrySet()) {
			UserInfo userInfo = new UserInfo();
			userInfo.setUserId(userId);
			userInfo.setInfoName(entry.getKey());
			userInfo.setInfoValue(entry.getValue());
			userInfoList.add(userInfo);
		}
		userInfoDao.batchSave(userInfoList);
	}

	@Override
	public Map<String, Object> getOne(String userId, UserInfoName infoName) {
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		userInfo.setInfoName(infoName.name());
		return userInfoDao.getOne(userInfo);
	}

	public void updateOne(String userId, UserInfoName userInfoName,
			String infoValue) {
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		userInfo.setInfoName(userInfoName.name());
		userInfo.setInfoValue(infoValue);
		userInfoList.add(userInfo);
		userInfoDao.batchSave(userInfoList);
	}

	@Override
	public void save(UserInfo userInfo) {
		userInfoDao.save(userInfo);
	}

	@Override
	public List<Long> queryUidsByLocation(String location) {
		
		return userInfoDao.queryUidsByLocation(location);
	}

	public Map<Long, Map<String, String>> findByUidsAndInfoNames(List<Long> uids, List<String> infoNames) {
		List<UserInfo> userInfos = userInfoDao.findByUidsAndInfoNames(uids,infoNames);
		Map<Long, Map<String, String>> map = Maps.newHashMap();
		for (UserInfo userInfo : userInfos) {
			Long uid = Long.valueOf(userInfo.getUserId());
			String infoName = userInfo.getInfoName();
			Map<String, String> tmpMap = map.get(uid);
			if (tmpMap == null) {
				tmpMap = Maps.newHashMap();
				map.put(uid, tmpMap);
			}
			tmpMap.put(infoName, userInfo.getInfoValue());
		}
		return map;
	}
}
